package com.taotao.search.controller;

import com.taotao.common.pojo.SearchItem;
import com.taotao.common.pojo.SearchResult;
import com.taotao.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SearchController {

    @Value("${ITEM_ROWS}")
    private Integer ITEM_ROWS;

    @Autowired
    private SearchService searchService;

    /**
     * 商品搜
     */
    @RequestMapping("/search")
    public String search(Integer page, @RequestParam(value = "q") String queryString, Model model) throws Exception {
        //get请求乱码问题
        queryString = new String(queryString.getBytes("iso-8859-1"), "utf-8");
        SearchResult searchResult = searchService.search(queryString, page, ITEM_ROWS);
        //封装数据
        model.addAttribute("query", queryString);
        model.addAttribute("totalPage", searchResult.getPageCount());
        model.addAttribute("itemList", searchResult.getItemList());
        model.addAttribute("page", page);

        for (SearchItem item : searchResult.getItemList()) {
            String sell_point = item.getSell_point();
            System.out.println(sell_point);
        }
        return "search";
    }
}
